package com.ygqh.baby.dao.impl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsCriteria;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import com.ygqh.baby.ao.Constant;
import com.ygqh.baby.dao.GridFSDao;

/**
 * 操作mongodb存储文件实现类
 * 
 */
@Repository("gridFSDao")
public class GridFSDaoImpl implements GridFSDao {

	@Autowired
	private GridFsTemplate gridFsTemplate;

	public GridFsTemplate getGridFsTemplate() {
		return gridFsTemplate;
	}

	/**
	 * 存储文件
	 * 
	 * @param content
	 *            文件内容
	 * @param filename
	 *            文件名
	 * @param contentType
	 *            内容类型
	 * @return
	 */
	@Override
	public GridFSFile store(InputStream content, String filename, String contentType) {
		return gridFsTemplate.store(content, filename, contentType);
	}

	/**
	 * 存储唯一文件
	 * 
	 * @param content
	 *            文件内容
	 * @param filename
	 *            文件名
	 * @param contentType
	 *            内容类型
	 * @param map
	 * 
	 * @return
	 */
	@Override
	public GridFSFile storeUnique(InputStream content, String filename, String contentType,
			HashMap<String, String> map) {

		List<GridFSDBFile> list = this.find(filename);
		for (GridFSDBFile gridFSDBFile : list) {
			this.delete(gridFSDBFile.getFilename(), gridFSDBFile.getContentType());
		}
		// 添加自定义字段
		BasicDBObjectBuilder basicDBObjectBuilder = BasicDBObjectBuilder.start();
		for (Entry<String, String> entry : map.entrySet()) {
			basicDBObjectBuilder.add(entry.getKey(), entry.getValue());
		}
		DBObject chuncDbObject = basicDBObjectBuilder.get();

		return gridFsTemplate.store(content, filename, contentType, chuncDbObject);
	}

	@Override
	public GridFSFile storeUnique(InputStream content, String filename, String contentType) {
		List<GridFSDBFile> list = this.find(filename);
		for (GridFSDBFile gridFSDBFile : list) {
			this.delete(gridFSDBFile.getFilename(), gridFSDBFile.getContentType());
		}

		return gridFsTemplate.store(content, filename, contentType);
	}

	/**
	 * 查询单个文件
	 * 
	 * @param filename
	 *            文件名
	 * @return
	 */
	@Override
	public GridFSDBFile findOne(String filename) {
		Query query = new Query(GridFsCriteria.whereFilename().in(filename));
		return gridFsTemplate.findOne(query);
	}

	/**
	 * 查询单个文件
	 * 
	 * @param filename
	 *            文件名
	 * @param contentType
	 *            文件类型
	 * @return
	 */
	@Override
	public GridFSDBFile findOne(String filename, String contentType) {
		Query query = new Query(GridFsCriteria.whereFilename().in(filename)
				.andOperator(GridFsCriteria.whereContentType().in(contentType)));
		return gridFsTemplate.findOne(query);
	}

	@Override
	public GridFSDBFile findOne(Criteria criteria) throws Exception {
		Validate.notNull(criteria, "criteria" + Constant.IS_NULL);

		Query query = new Query();
		query.addCriteria(criteria);
		return gridFsTemplate.findOne(query);
	}

	/**
	 * 查询所有文件
	 * 
	 * @param filename
	 *            文件名
	 * @return
	 */
	@Override
	public List<GridFSDBFile> find(String filename) {
		Query query = new Query(GridFsCriteria.whereFilename().in(filename));
		return gridFsTemplate.find(query);
	}

	/**
	 * 查询所有文件
	 * 
	 * @param filename
	 *            文件名
	 * @param contentType
	 *            文件类型
	 * @return
	 */
	@Override
	public List<GridFSDBFile> find(String filename, String contentType) {
		Query query = new Query(GridFsCriteria.whereFilename().in(filename)
				.andOperator(GridFsCriteria.whereContentType().in(contentType)));
		return gridFsTemplate.find(query);
	}

	@Override
	public List<GridFSDBFile> find(Criteria criteria) throws Exception {
		Validate.notNull(criteria, "criteria" + Constant.IS_NULL);

		Query query = new Query();
		query.addCriteria(criteria);

		return gridFsTemplate.find(query);
	}

	/**
	 * 删除文件
	 * 
	 * @param key
	 *            id
	 */
	@Override
	public void deleteById(String key) {
		Query query = new Query(GridFsCriteria.where("_id").is(key));

		gridFsTemplate.delete(query);
	}

	/**
	 * 删除文件
	 * 
	 * @param filename
	 *            文件名
	 * @param contentType
	 *            文件类型
	 */
	@Override
	public void delete(String filename, String contentType) {
		Query query = new Query(GridFsCriteria.whereFilename().in(filename)
				.andOperator(GridFsCriteria.whereContentType().in(contentType)));
		gridFsTemplate.delete(query);
	}

	@Override
	public GridFSDBFile findById(String id) throws Exception {
		Query query = new Query(GridFsCriteria.where("_id").is(id));
		return gridFsTemplate.findOne(query);
	}

}
